/**
 * 自动生成 SysFunc
 * 
 * Public/js/backend/sysfunc.js
 * admin @ 2015-06-10
 */

var SysFunc = {
  config: {
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);

    loading();
    _this.init_btn();
    _this.init_grid();
  },
    
  init_btn: function () {
    var _this = this;

    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TreeGrid('getparam');
      query_param.page = 1;

      $('#data_table').TreeGrid('param', query_param);
      $('#data_table').TreeGrid('reload');
    });

    
    //打开添加窗口
    $(document).on('click', '.load_add_modal_btn', function (e) {
      e.preventDefault();
      _this.config.action = 'create';
      $('#submit_form').attr('action', _this.config.create_url);
      $('#submit_form')[0].reset();
      fillform( $('#submit_form') , _this.config.row );
      
      $('#action_label').html('新增');
      $('#add_edit_modal').modal('show');
    });

    //打开编辑窗口
    $(document).on('click', '.edit_btn', function (e) {
      e.preventDefault();
      
      var id = $(this).data( 'id' );
      var data = $('#data_table').TreeGrid('getrow' , id );

      fillform( $('#submit_form') , data );
      $('#submit_form').attr('action', _this.config.update_url + '/' + data['id'] );
      _this.config.action = 'update';
            
      $('#action_label').html('编辑');
      $('#add_edit_modal').modal('show'); 
      
    });

    //提交
    $(document).on('click', '#submit_btn', function (e) {
      e.preventDefault();

      var _thisbtn = $(this);
      var url = $('#submit_form').attr('action');
      var data = $('#submit_form').serializeObject();

      if (validform($('#submit_form'))) {
        $.post(url, data, function (ret) {
          tips(ret.info) ;
          if (ret.status) {
            $('#add_edit_modal').modal('hide'); 
            var query_param = $('#data_table').TreeGrid('getparam');
            query_param.page = 1 ;
            $('#data_table').TreeGrid('param' , query_param);
            $('#data_table').TreeGrid('reload');
          }
        });
      }
    });
    
    //操作页面
    $(document).on('click', '.load_operation_btn', function (e) {
      e.preventDefault();
      var id = $(this).data( 'id' );
      window.location.href = _this.config.sys_operation_url + id ;
    });

    //单个删除按钮
    $(document).on('click', '.del_one_btn', function (e) {
      e.preventDefault();
      var id = $(this).data( 'id' );
      var data = $('#data_table').TreeGrid('getrow' , id );
      _this.config.del_id = data['id'];
      _this.del_data();
    });
  },
  
  init_grid: function () {
    var _this = this ;
    $('#data_table').TreeGrid({
      field : 'name',
      url : _this.config.read_url ,
      row_style : function( row , index ){
        if ( row.status != 1 ) {
            return 'info';
        }
      }
    });
  },
  
  del_data: function (ids) {
    var _this = this;

    sure.init('确定删除吗？', function () {
      var url = _this.config.delete_url;
      var data = {ids: _this.config.del_id};

      $.post(url, data, function (ret) {
        tips(ret.info);
        if (ret.status) {
          $('#data_table').TreeGrid('reload');
        }
      });
    });
  },
};



var format_fa = function(value , row , index) {
  var html = empty( value ) ? '' : '<i class="fa '+ value +'" style="opacity:0.5;font-size:16px; width:18px;"></i>';
  return html ; 
}

var format_operation = function(value , row , index) {
  return empty(value) ? '' : value ;
}

var opt_operation = function(value , row , index) {
	return '<a class="btn btn-info load_operation_btn" data-id="' + row.id
			+ '" href="#" taget="_blank" title="添加操作"><i class="fa fa-wrench "></i> 操作管理</a>';
}







